﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Zenntrix_Toolkit;
using Vara_Data_Layer.Vara_Data_Categories;
using Vara_Data_Layer.Vara_Data_Holder;
using Vara_Data_Layer.Vara_Data_Make_Model;
using Vara_Data_Layer.Vara_Data_Location;
using Vara_Data_Layer.Vara_Data_Additional_Option;
using Vara_Data_Layer.Vara_Data_Audit_Schedule;
using Xentrix;

namespace Vara_Data_Layer.Vara_Data_Asset
{
    public class Vara_Data_Asset_Controller : Vara_Data_IAsset_Actions
    {
        public void InsertAsset(Vara_Data_IAsset Asset)
        {
            if (Asset.ID > 0)
            {
                using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
                {
                    NewSqlConnector.ExecuteProcedure("Insert_Asset",
                        new SqlEngineProcedureVariable("@ID", Asset.ID.ToString()));
                }
            }
        }

        public void UpdateAsset(Vara_Data_IAsset Asset)
        {
            if (Asset.ID > 0)
            {
                using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
                {
                    SqlEngineProcedureVariable[] var = { 
                                                        new SqlEngineProcedureVariable("@ID", Asset.ID, SqlDbType.Int),
                                                        new SqlEngineProcedureVariable("@DisplayName", Asset.DisplayName.ToString()),
                                                        new SqlEngineProcedureVariable("@AssetLabel", Asset.AssetLabel.ToString()),
                                                        new SqlEngineProcedureVariable("@SerialNumber", Asset.SerialNumber.ToString()),
                                                        new SqlEngineProcedureVariable("@Initial_Audit_Date", Asset.InitialAuditDate, SqlDbType.DateTime),
                                                        new SqlEngineProcedureVariable("@Makes_ID",Asset.Make, SqlDbType.Int),
                                                        new SqlEngineProcedureVariable("@Models_ID", Asset.Model, SqlDbType.Int)
                                                       };
                    NewSqlEngine.ExecuteProcedure("Update_Asset_Details", var);
                }
            }
        }

        public void DeleteAsset(Vara_Data_IAsset Asset)
        {
            throw new NotImplementedException();
        }

        public Vara_Data_IAsset ReturnAsset(int ID)
        {
            DataRow DataRowAssetInformation;
            DataTable DataTableAssetCategories;
            DataTable DataTableAssetAdditionalOptions;
            using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
            {
                DataRowAssetInformation = NewSqlConnector.ExecuteProcedure("Return_Asset",
                                                    new SqlEngineProcedureVariable("@ID", ID.ToString())).Rows[0];
                DataTableAssetCategories = NewSqlConnector.ExecuteProcedure("Return_Asset_Categories",
                                                    new SqlEngineProcedureVariable("@AssetID", ID.ToString()));
                DataTableAssetAdditionalOptions = NewSqlConnector.ExecuteProcedure("Return_Asset_Additional_Options",
                                                    new SqlEngineProcedureVariable("@AssetID", ID.ToString()));
            }

            Vara_Data_IAsset AssetToReturn = new Vara_Data_Asset(ID);

            #region /* Set AssetToReturn Properties */
            AssetToReturn.DisplayName = DataRowAssetInformation["DisplayName"].ToString();
            AssetToReturn.AssetLabel = DataRowAssetInformation["AssetLabel"].ToString();
            AssetToReturn.SerialNumber = DataRowAssetInformation["SerialNumber"].ToString();
            if (DataRowAssetInformation["Makes_ID"].ToString() != "")
                AssetToReturn.Make = new Vara_Data_Make_Controller().ReturnMake(int.Parse(DataRowAssetInformation["Makes_ID"].ToString()));
            if (DataRowAssetInformation["Models_ID"].ToString() != "")
                AssetToReturn.Model = new Vara_Data_Model_Controller().ReturnModel(int.Parse(DataRowAssetInformation["Models_ID"].ToString()));
            AssetToReturn.Description = DataRowAssetInformation["Description"].ToString();
            AssetToReturn.Owner = DataRowAssetInformation["AssetOwner"].ToString();
            if (DataRowAssetInformation["AssetHolder"].ToString() != "")
                AssetToReturn.Holder = new Vara_Data_Holder_Controller().ReturnHolder(int.Parse(DataRowAssetInformation["AssetHolder"].ToString()));
            if (DataRowAssetInformation["LocationID"].ToString() != "")
                AssetToReturn.Location = new Vara_Data_InternalLocation_Controller().ReturnLocation(int.Parse(DataRowAssetInformation["LocationID"].ToString()));
            if (DataRowAssetInformation["Lookup_Audit_Schedule_ID"].ToString() != "")
                AssetToReturn.AuditSchedule = new Vara_data_Audit_Schedule_Controller().ReturnAuditSchedule(int.Parse(DataRowAssetInformation["Lookup_Audit_Schedule_ID"].ToString()));
            AssetToReturn.InitialAuditDate = DateTime.Parse(DataRowAssetInformation["Initial_Audit_Date"].ToString());
            List<Vara_Data_ICategory> AssetCategories = new List<Vara_Data_ICategory>();
            foreach (DataRow R in DataTableAssetCategories.Rows)
            {
                AssetCategories.Add(new Vara_Data_Category_Controller().ReturnCategory(int.Parse(R["ID"].ToString())));
            }
            AssetToReturn.Categories = AssetCategories;
            List<Vara_Data_IAdditional_Option> AssetAdditionalOptions = new List<Vara_Data_IAdditional_Option>();
            foreach (DataRow R in DataTableAssetAdditionalOptions.Rows)
            {
                AssetAdditionalOptions.Add(new Vara_Data_Additional_Option_Controller().ReturnAdditionalOption(int.Parse(R["Additional_Options_ID"].ToString())));
            }
            AssetToReturn.AdditionalOptions = AssetAdditionalOptions;
            #endregion

            return AssetToReturn;
        }

        public void Refresh_Assets_List()
        {
            throw new NotImplementedException();
        }

        public List<Vara_Data_IAsset> Assets
        {
            get { throw new NotImplementedException(); }
        }

        public DataTable SearchAssets(string searchCriteria, int startIndex, int pageSize, string sortBy)
        {
            DataTable DataTableSearchResults;
            using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
            { 
               SqlEngineProcedureVariable[] var = {
                                                            new SqlEngineProcedureVariable("@SearchCriteria",searchCriteria.ToString()),
                                                            new SqlEngineProcedureVariable("@StartIndex",startIndex, SqlDbType.Int),
                                                            new SqlEngineProcedureVariable("@PageSize", pageSize, SqlDbType.Int),
                                                            new SqlEngineProcedureVariable("@SortBy", sortBy.ToString())
                                                   };
               DataTableSearchResults = NewSqlEngine.ExecuteProcedure("Search_Assets_Paging", var);
            }
            return DataTableSearchResults;
        }
    }
}
